﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DotSpatial.Controls;
using DotSpatial.Controls.RibbonControls;
using System.Windows.Forms;
using Oatc.OpenMI.Gui.ConfigurationEditor;
using System.Drawing;
using System.IO;
using System.Reflection;
using HydroDesktop.Help;



namespace Oatc.OpenMI.Gui.ConfigurationEditor
{
    [Plugin("HydroModeler", Author= "University of South Carolina", UniqueName="mw_HydroModeler_1",Version="1.4")]
    public class Program :Extension, IMapPlugin 
    {
        #region Variables

        //reference to the main application and it's UI items
        private IMapPluginArgs _mapArgs;
        public RibbonTab _ribbonTab;
        public RibbonButton _ribbonBtn;
        private ITabManager _t;
        public string _panelName = "HydroModeler";
        mainTab hydroModelerControl;
        Bitmap bmp;
        List<RibbonPanel> rps;
        private string ImagePath = Path.GetDirectoryName(Assembly.GetAssembly(typeof(Program)).CodeBase).Remove(0,6) + "/icons";
        private int tabID = 0;
        private readonly string _localHelpUri = HydroModeler.Settings.Default.localHelpUri;

        #endregion


        #region IExtension Members

        protected override void OnDeactivate()
        {

            // Remove ribbon tab
            if (_mapArgs.Ribbon.Tabs.Contains(_ribbonTab))
            {
                _mapArgs.Ribbon.Tabs.Remove(_ribbonTab);
            }

            // Remove ribbon button in view panel
            _mapArgs.Ribbon.Tabs[0].Panels[0].Items.Remove(_ribbonBtn);

            // Remove Work Page
            if (_t.Contains(_panelName))
                _t.RemoveTab(_panelName);

            // This line ensures that "Enabled" is set to false.
            base.OnDeactivate();

        }
        #endregion

        public List<RibbonPanel> Rps
        {
            get { return rps; }
            set { rps = value; }
        }

        #region IMapPlugin Members

        public void Initialize(IMapPluginArgs args)
        {
            //set map args
            _mapArgs = args;

            // Initialize the Ribbon controls in the "Ribbon" ribbon tab
            _ribbonTab = new RibbonTab(_mapArgs.Ribbon, _panelName);
            _ribbonTab.ActiveChanged +=new EventHandler(_ribbonTab_ActiveChanged);

            //Add Buttons to the Ribbon Panel
            rps = BuildRibbonPanel();
            foreach (RibbonPanel rp in rps)
                _ribbonTab.Panels.Add(rp);

             //Add TabPage to the MwTabControl
            _t = _mapArgs.PanelManager;

            //specify tab window
            hydroModelerControl = new mainTab(args, rps, ((RibbonTextBox)((RibbonItemGroup)rps[2].Items[0]).Items[0]).TextBoxText);
            _t.AddTab(_panelName, hydroModelerControl, bmp);
            
            //activate tab
            _ribbonBtn_Click(this, EventArgs.Empty);

            //update filelist
            string text = ((RibbonTextBox)((RibbonItemGroup)rps[2].Items[0]).Items[0]).TextBoxText;
            hydroModelerControl.UpdateFileList(text);
          
        }

        #endregion




        List<RibbonPanel> BuildRibbonPanel()
        {
            //Create a new Ribbon Panel
            RibbonPanel menu_panel = new RibbonPanel("Model",RibbonPanelFlowDirection.Bottom);
            RibbonPanel model_tools = new RibbonPanel("Composition", RibbonPanelFlowDirection.Bottom);
            RibbonPanel dir_panel = new RibbonPanel("Current Directory", RibbonPanelFlowDirection.Bottom);
            RibbonPanel utility_panel = new RibbonPanel("Utilities", RibbonPanelFlowDirection.Bottom);
            RibbonPanel help_panel = new RibbonPanel("Help", RibbonPanelFlowDirection.Bottom);
            
            Bitmap b;
            Bitmap b_small;
            Graphics g;

            #region menu panel
            menu_panel.ButtonMoreVisible = false;
            //Open Composition
            RibbonButton rb = new RibbonButton("Open");
            menu_panel.Items.Add(rb);

            Image open = Bitmap.FromFile(ImagePath + "\\open.png");
            b = new Bitmap(32, 32);
            g = Graphics.FromImage((Image)b);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            g.DrawImage(open, 0, 0, 32, 32);
            rb.Image = b;

            b_small = new Bitmap(20, 20);
            g = Graphics.FromImage((Image)b_small);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            open = Bitmap.FromFile(ImagePath + "\\open.png");
            g.DrawImage(open, 0, 0, 20, 20);
            rb.SmallImage = b_small;

            rb.Click += new EventHandler(OpenComp_Click);
            g.Dispose();

            //save
            rb = new RibbonButton("Save");
            menu_panel.Items.Add(rb);

            b = new Bitmap(32, 32);
            g = Graphics.FromImage((Image)b);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            Image save = Bitmap.FromFile(ImagePath + "\\save.png");
            g.DrawImage(save, 0, 0, 32, 32);
            rb.Image = b;

            b_small = new Bitmap(20, 20);
            g = Graphics.FromImage((Image)b_small);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            save = Bitmap.FromFile(ImagePath + "\\save.png");
            g.DrawImage(save, 0, 0, 20, 20);
            rb.SmallImage = b_small;

            rb.Click += new EventHandler(Save_Click);
            g.Dispose();

            //save as
            rb = new RibbonButton();
            rb.Text = "Save As...";
            menu_panel.Items.Add(rb);

            b = new Bitmap(32, 32);
            g = Graphics.FromImage((Image)b);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            Image saveas = Bitmap.FromFile(ImagePath + "\\saveas.png");
            g.DrawImage(saveas, 0, 0, 32, 32);
            rb.Image = b;

            b_small = new Bitmap(20, 20);
            g = Graphics.FromImage((Image)b_small);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            saveas = Bitmap.FromFile(ImagePath + "\\saveas.png");
            g.DrawImage(saveas, 0, 0, 20, 20);
            rb.SmallImage = b_small;

            rb.Click += new EventHandler(SaveAs_Click);
            g.Dispose();

            #endregion

            #region model_panel
            model_tools.ButtonMoreVisible = false;
            //Add Model
            rb = new RibbonButton("Add Component");
            model_tools.Items.Add(rb);
            rb.ToolTip = "Click to add a model to the composition";

            b = new Bitmap(32, 32);
            g = Graphics.FromImage((Image)b);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            Image model = Bitmap.FromFile(ImagePath + "\\add_model.png");
            g.DrawImage(model, 0, 0, 32, 32);
            rb.Image = b;

            b_small = new Bitmap(20, 20);
            g = Graphics.FromImage((Image)b_small);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            model = Bitmap.FromFile(ImagePath + "\\add_model.png");
            g.DrawImage(model, 0, 0, 20, 20);
            rb.SmallImage = b_small;

            rb.Click += new EventHandler(AddModel_Click);
            g.Dispose();

            //Add Trigger
            rb = new RibbonButton("Add Trigger");
            model_tools.Items.Add(rb);

            b = new Bitmap(32, 32);
            g = Graphics.FromImage((Image)b);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            Image trigger = Bitmap.FromFile(ImagePath + "\\trigger.png");
            g.DrawImage(trigger, 0, 0, 32, 32);
            rb.Image = b;

            b_small = new Bitmap(20, 20);
            g = Graphics.FromImage((Image)b_small);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            model = Bitmap.FromFile(ImagePath + "\\trigger.png");
            g.DrawImage(model, 0, 0, 20, 20);
            rb.SmallImage = b_small;

            rb.Click += new EventHandler(AddTrigger_Click);
            g.Dispose();

            //Add Connection
            rb = new RibbonButton("Add Connection");
            rb.Enabled = false;
            model_tools.Items.Add(rb);

            b = new Bitmap(32, 32);
            g = Graphics.FromImage((Image)b);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            Image link = Bitmap.FromFile(ImagePath + "\\add_connection.png");
            g.DrawImage(link, 0, 0, 32, 32);
            rb.Image = b;

            b_small = new Bitmap(20, 20);
            g = Graphics.FromImage((Image)b_small);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            model = Bitmap.FromFile(ImagePath + "\\add_connection.png");
            g.DrawImage(model, 0, 0, 20, 20);
            rb.SmallImage = b_small;

            rb.Click += new EventHandler(AddConnection_Click);
            g.Dispose();


            //Run
            rb = new RibbonButton("Run");
            rb.Enabled = false;
            model_tools.Items.Add(rb);

            b = new Bitmap(32, 32);
            g = Graphics.FromImage((Image)b);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            Image run = Bitmap.FromFile(ImagePath + "\\run.png");
            g.DrawImage(run, 0, 0, 32, 32);
            rb.Image = b;

            b_small = new Bitmap(20, 20);
            g = Graphics.FromImage((Image)b_small);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            model = Bitmap.FromFile(ImagePath + "\\run.png");
            g.DrawImage(model, 0, 0, 20, 20);
            rb.SmallImage = b_small;

            rb.Click += new EventHandler(Run_Click);
            g.Dispose();

            //Clear Composition
            rb = new RibbonButton("Clear Composition");
            model_tools.Items.Add(rb);

            b = new Bitmap(32, 32);
            g = Graphics.FromImage((Image)b);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            Image delete = Bitmap.FromFile(ImagePath + "\\delete_icon.png");
            g.DrawImage(delete, 0, 0, 32, 32);
            rb.Image = b;

            b_small = new Bitmap(20, 20);
            g = Graphics.FromImage((Image)b_small);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            model = Bitmap.FromFile(ImagePath + "\\delete_icon.png");
            g.DrawImage(model, 0, 0, 20, 20);
            rb.SmallImage = b_small;

            rb.Click += new EventHandler(this.clear_Click);
            rb.Dispose();



            #endregion

            #region dir_panel
            dir_panel.ButtonMoreVisible = false;
            rb = new RibbonButton();
            rb.MaxSizeMode = RibbonElementSizeMode.Compact;
            open = Bitmap.FromFile(ImagePath + "\\change_dir.png");
            b = new Bitmap(20, 20);
            g = Graphics.FromImage((Image)b);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            g.DrawImage(open, 0, 0, 20, 20);
            rb.SmallImage = b;
            rb.MinSizeMode = RibbonElementSizeMode.Compact;
            rb.Click += new EventHandler(this.dirItem_click);
            rb.Dispose();


            RibbonTextBox rtb = new RibbonTextBox();
            rtb.Text = "Current Directory";
            rtb.TextBoxText = Path.GetFullPath(ImagePath + "/../example_configuration");
            rtb.TextBoxWidth = 300;
            rtb.TextBoxTextChanged += new System.EventHandler(ribbonTextBox_textChanged);



            //dir_panel.Items.Add(rtb);

            RibbonItemGroup rig = new RibbonItemGroup();
            rig.Items.Add(rtb);
            rig.Items.Add(rb);

            dir_panel.Items.Add(rig);
            #endregion

            #region help_panel
            rb = new RibbonButton();

            Image help = Bitmap.FromFile(ImagePath + "\\help.png");
            b = new Bitmap(20, 20);
            g = Graphics.FromImage((Image)b);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            g.DrawImage(help, 0, 0, 20, 20);
            rb.Image = b;
            rb.SmallImage = b;

            rb.Click += new EventHandler(this.getHelp);
            g.Dispose();
            help_panel.Items.Add(rb);
            #endregion

            List<RibbonPanel> l = new List<RibbonPanel>();
            l.Add(menu_panel);
            l.Add(model_tools);
            l.Add(dir_panel);
            l.Add(help_panel);

            return l;
        }

        void reloadButtons(bool run, bool connection)
        {
            
            //enable or disable run button
            rps[1].Items[3].Enabled = run;

            //enable or disble connection button
            rps[1].Items[2].Enabled = connection;

        }


        #region UI Events
        private void _ribbonTab_ActiveChanged(object sender, EventArgs e)
        {
            if(_ribbonTab.Selected)
                _ribbonBtn_Click(sender, e);
        }
        private void ribbonTextBox_textChanged(object sender, EventArgs e)
        {
            //get path from ribbon textbox
           string path = ((RibbonTextBox)((RibbonItemGroup)rps[2].Items[0]).Items[0]).TextBoxText;

            //replace path slashes
            path = path.Replace('/', '\\');

            //update file list
            hydroModelerControl.UpdateFileList(path);
        }
        public void ribbonTextBox_update(string path)
        {
            //update the ribbon textbox text
            ((RibbonTextBox)((RibbonItemGroup)rps[2].Items[0]).Items[0]).TextBoxText = path;
        }
        public void dirItem_click(object sender, EventArgs e)
        {
            string path = hydroModelerControl.changeDir_Click(sender, e);

            if(path != null)
            {
                ribbonTextBox_update(path);
            }
        }
        void _ribbonBtn_Click(object sender, EventArgs e)
        {   
            if (_t.SelectedTabName == _panelName)
            {
                if (!_mapArgs.Ribbon.Tabs.Contains(_ribbonTab))
                {
                    _mapArgs.Ribbon.Tabs.Add(_ribbonTab);
                    _t.SelectedTabName = _panelName;
                    _mapArgs.Ribbon.ActiveTab = _mapArgs.Ribbon.Tabs[tabID];
                }
            }
            else
            {
                if (!_mapArgs.Ribbon.Tabs.Contains(_ribbonTab))
                {
                    _mapArgs.Ribbon.Tabs.Add(_ribbonTab);
                    tabID = _mapArgs.Ribbon.Tabs.Count - 1;
                    _t.SelectedTabName = _panelName;
                    _mapArgs.Ribbon.ActiveTab = _mapArgs.Ribbon.Tabs[tabID];
                }
                else
                {
                    hydroModelerControl.mainTab_KeyPress(this,new KeyPressEventArgs((char)System.Windows.Forms.Keys.LButton));
                    _t.SelectedTabName = _panelName;
                    _mapArgs.Ribbon.ActiveTab = _mapArgs.Ribbon.Tabs[tabID];
                }
            } 
        }
        void keypressed(Object o, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Return)
            {
                e.Handled = true;
            }
        }
        void OpenComp_Click(object sender, EventArgs e)
        {
            //call open composition event
            hydroModelerControl.menuFileOpen_Click(sender, e);
        }
        void Save_Click(object sender, EventArgs e)
        {
            //call save event
            hydroModelerControl.menuFileSave_Click(sender, e);
        }
        void SaveAs_Click(object sender, EventArgs e)
        {
            //call save as event
            hydroModelerControl.menuFileSaveAs_Click(sender, e);
         
        }
        void AddModel_Click(object sender, EventArgs e)
        {
            //call add model event
            hydroModelerControl.menuEditModelAdd_Click(sender, e);
        }
        void AddConnection_Click(object sender, EventArgs e)
        {
            //call add connection event
            hydroModelerControl.menuEditConnectionAdd_Click(sender, e);
        }
        void AddTrigger_Click(object sender, EventArgs e)
        {
            //call add trigger event
            hydroModelerControl.menuEditTriggerAdd_Click(sender, e);
        }
        void Run_Click(object sender, EventArgs e)
        {
            //call run event
            hydroModelerControl.menuDeployRun_Click(sender, e);
        }
        void clear_Click(object sender, EventArgs e)
        {
            DialogResult result =  MessageBox.Show("Are you sure you want to remove all models from the composition window?", "Important Question", MessageBoxButtons.YesNo); 
            if(result == DialogResult.Yes)
                hydroModelerControl.clear();
        }
        void getHelp(object sender, EventArgs e)
        {
            LocalHelp.OpenHelpFile(_localHelpUri);
        }
        #endregion
    }
}
